import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/util/logger.dart';

import '../const/Const.dart';
import '../http/model/video_model.dart';
import '../navigator/hi_navigator.dart';
import '../provider/theme_provider.dart';

class VideoCard extends StatelessWidget {
  final VideoModel data;

  const VideoCard(this.data, {super.key});

  @override
  Widget build(BuildContext context) {
    return _buildCard(context, data);
  }

  Widget _buildCard(BuildContext context, VideoModel data) {
    return InkWell(
      onTap: () {
        logger.i(data.url);
        HiNavigator.getInstance().onJumpTo(RouteStatus.detail,
            args: {Const.VIDEO_ID: data.vid, Const.VIDEO_MODEL: data});
      },
      child: SizedBox(
        height: 200,
        child: Card(
          margin: const EdgeInsets.only(left: 4, right: 4, bottom: 8),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _itemImage(context, data.cover, 120),
                _buildInfo(context, data)
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _itemImage(BuildContext context, String url, double height) {
    var size = MediaQuery.of(context).size;
    return Stack(
      children: [
        CachedNetworkImage(
          imageUrl: url,
          width: size.width / 2 - 10,
          height: height,
          fit: BoxFit.cover,
          errorWidget: (context, url, error) => const Icon(Icons.error),
        )
      ],
    );
  }

  Widget _buildInfo(BuildContext context, VideoModel data) {
    return Expanded(
        child: Container(
      padding: const EdgeInsets.only(top: 5, left: 8, right: 8, bottom: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            data.title,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 12,
                color: isDarkTheme(context) ? Colors.grey : Colors.black87),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  ClipOval(
                    child: Image.network(
                      data.owner.face,
                      width: 10,
                      height: 10,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 8),
                    child: Text(
                      data.owner.name,
                      style: TextStyle(
                          fontSize: 11,
                          color: isDarkTheme(context)
                              ? Colors.grey
                              : Colors.black87),
                      overflow: TextOverflow.ellipsis, // 处理溢出内容
                    ),
                  ),
                ],
              ),
              const Icon(
                Icons.more_vert_sharp,
                size: 15,
                color: Colors.grey,
              )
            ],
          )
        ],
      ),
    ));
  }
}
